
//#include "main.h"
#include <pic18.h>
#include "Outputs.h"
#include "SysTime.h"
#include "hardware.h" 

#ifndef   OUTPUT
#define   OUTPUT		0
#endif



#define   HIGH  	1
#define   LOW		0


unsigned int OUT_1;
unsigned int OUT_2;
unsigned int OUT_3;
unsigned int OUT_4;
unsigned int OUT_5;


unsigned char BlinkTimer = DEFAULT_BLINK_CYCLE;

bit Blink;
unsigned char BlinkCycle;

void out_set ( unsigned char out_name, unsigned int pulse_duration )
{
	switch( out_name )
	{
		case 1:
			OUT_1 = pulse_duration;
			break;

		case 2:
			OUT_2 = pulse_duration;
			break;
			
		case 3:
			OUT_3 = pulse_duration;
			break;

		case 4:
			OUT_4 = pulse_duration;
			break;

		case 5:
			OUT_5 = pulse_duration;
			break;

		default:
			;
	}  
}


void Outputs_Init( void )
{
	PIN_RELAY_1_DIR = OUTPUT;
	PIN_RELAY_2_DIR = OUTPUT;

	PIN_OUT_3_DIR = OUTPUT;
	PIN_OUT_4_DIR = OUTPUT;
	PIN_OUT_5_DIR = OUTPUT;

	OUT_1 = LOW;
	OUT_2 = LOW;
	OUT_3 = LOW;
	OUT_4 = LOW;
	OUT_5 = HIGH;					  /* RF2 */
	
	BlinkCycle = DEFAULT_BLINK_CYCLE;
	
	Drive_Outputs();
}


void Drive_Outputs( void )
{
//	static unsigned char t1, t2, t3, t4;

	if ( Time_100ms && --BlinkTimer == 0 )  
	{
		BlinkTimer = BlinkCycle;
		
		Blink ^= 1;
	}
	
	switch ( OUT_1 )
	{
		case LOW:
			PIN_RELAY_1 = 0;
			break;

		case HIGH:
			PIN_RELAY_1 = 1;
/* 			RG3 = 0; */
			break;
		
		case OUT_BLINK:
			PIN_RELAY_1 = Blink;
			break;


		default:
			PIN_RELAY_1 = HIGH;
			if ( Time_100ms  )
			{
	/* 			RG3 = 1;				  /\* RELAY *\/ */

				if ( --OUT_1 == 1 )
					OUT_1 = 0;
			}
	}

	switch ( OUT_2 )
	{
		case LOW:
			PIN_RELAY_2 = 0;
			break;

		case HIGH:
			PIN_RELAY_2 = 1;
			break;
		
		case OUT_BLINK:
			PIN_RELAY_2 = Blink;
			break;


		default:
			PIN_RELAY_2 = HIGH;
			if ( Time_100ms  )
			{
				if ( --OUT_2 == 1 )
					OUT_2 = 0;
			}
	}

	switch ( OUT_3 )
	{
		case LOW:
			PIN_OUT_3 = 0;
			break;

		case HIGH:
			PIN_OUT_3 = 1;
			break;
		
		case OUT_BLINK:
			PIN_OUT_3 = Blink;
			break;


		default:
			PIN_OUT_3 = LOW;
			if ( Time_100ms  )
			{
				OUT_3--;
			}
	}

						
	switch ( OUT_4 )
	{
		case LOW:
			PIN_OUT_4 = 0;
			break;

		case HIGH:
			PIN_OUT_4 = 1;
			break;
		
		case OUT_BLINK:
			PIN_OUT_4 = Blink;
			break;


		default:
			PIN_OUT_4 = LOW;
			if ( Time_100ms  )
			{	
				OUT_4--;
			}
	}

	switch ( OUT_5 )
	{
		case LOW:
			PIN_OUT_5 = 0;
			break;

		case HIGH:
			PIN_OUT_5 = 1;
			break;
		
		case OUT_BLINK:
			PIN_OUT_5 = Blink;
			break;


		default:
			PIN_OUT_5 = LOW;
			if ( Time_100ms  )
			{	
				OUT_5--;
			}
	}

}
